package com.retail.proxy.controller;

import cn.hutool.core.util.ObjectUtil;
import com.retail.core.common.AbstractCommonController;
import com.retail.core.entity.ParamReq;

import com.retail.core.entity.result.PageResult;
import com.retail.core.entity.result.Result;
import com.retail.global.security.LoginUser;
import com.retail.global.security.LoginUserUtil;
import com.retail.global.annon.TraceLog;
import com.retail.proxy.entity.request.SysAdminProxyUpgradeRequest;
import com.retail.proxy.entity.vo.SysAdminProxyUpgradeVo;
import com.retail.proxy.service.SysAdminProxyUpgradeService;
import com.retail.system.entity.request.AdminRequest;
import com.retail.system.enums.UserTypeEnums;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代理晋升 API
 *
 * @author Administrator
 */
@Api(tags = "代理晋升 API")
@RestController
@RequestMapping("/sysAdminProxyUpgrade")
public class SysAdminProxyUpgradeController extends AbstractCommonController<SysAdminProxyUpgradeService, SysAdminProxyUpgradeRequest, SysAdminProxyUpgradeVo> {

    @Autowired
    private SysAdminProxyUpgradeService sysAdminProxyUpgradeService;

    @TraceLog()
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(sysAdminProxyUpgradeService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("保存代理晋升")
    public Result<Integer> save(@RequestBody SysAdminProxyUpgradeRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.save(sysAdminProxyUpgradeService, request);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新代理晋升")
    public Result<Integer> update(@RequestBody SysAdminProxyUpgradeRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.update(sysAdminProxyUpgradeService, request);
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("代理晋升-查询")
    public Result<List<SysAdminProxyUpgradeVo>> listPage(@RequestBody ParamReq req) {
        return super.listPage(sysAdminProxyUpgradeService, req);
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("代理晋升-统计")
    public Result<Integer> countPage(@RequestBody ParamReq req) {
        return super.countPage(sysAdminProxyUpgradeService, req);
    }

    @TraceLog()
    @PostMapping("page")
    @ApiOperation("代理晋升-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<SysAdminProxyUpgradeRequest> req) {

        SysAdminProxyUpgradeRequest adminRequest = req.getEntity();
        Long adminId = getLoginUserId();
        LoginUser loginUser = LoginUserUtil.getPrincipal();
        if (ObjectUtil.equal(adminRequest.getUserType(), UserTypeEnums.BUSINESS_TYPE.getValue())) {
            if (loginUser.getUserType().equals(UserTypeEnums.SYSTEM_MANAGER_TYPE.getValue())) {
                adminRequest.setSearchOwnerUser(2);
            } else {
                adminRequest.setSearchOwnerUser(1);
                adminRequest.setSearchOwnerUserId(adminId);
            }
        }
        req.setEntity(adminRequest);
        return super.page(sysAdminProxyUpgradeService, req);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除代理晋升")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return super.delete(sysAdminProxyUpgradeService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除代理晋升")
    @DeleteMapping("{ids}")
    public Result<Integer> batchDelete(List<Long> ids) {
        return super.batchDelete(sysAdminProxyUpgradeService, ids);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }

}
